import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import {
  AssetCabinet,
  ElectricData,
  ElectricDataRealTimeLast,
} from "@/api/backend/index";
import qs from "qs";

export const useElectricData = defineStore(Names.ElectricData, {
  state: () => {
    return {
      type: <any>[],
      single: <any>{},
      tags: <any>{},
      horizontalFields: <any>[], //卧式机柜字段组合
      typeCategoryFormat: <any>[], //不同类别字段的数据格式
      verticalFields: <any>[], //立式机柜字段组合
      history: "温度",
      realTimetype: "卧式",
      cabinetId: null, //查询的机柜id
      beginTime: "",
      endTime: "",
      isAsc: "desc", //升降序 默认倒序
      orderByColumn: "createTime", //排序指定字段 默认createTime
      endData: <any>{}, //获取的历史记录所有字段的数组数据
      xAxisData: <any>[], //x轴坐标
      sites: <any>{}, //站点map
      siteIds: <any>[], //站点id数组
      typeMode: <any>[], //5g小脑工作模式字典
      typefp1: <any>[], //获取5g小脑泵状态字典
      typewf1: <any>[], //获取5g小脑风机状态字典
      typepf1: <any>[], //获取5g小脑泵阀状态字典
      typemode1: <any>[], //获取5g小脑自动模式状态字典
      typeqf: <any>[], //获取泄压阀状态字典
    };
  },
  getters: {},
  actions: {
    //获取机柜管理列表数据
    getAssetCabinetList() {
      return new Promise((resolve, reject) => {
        _get(AssetCabinet + "/list", { pageSize: 100000 }, "json", "")
          .then((res: any) => {
            console.log("获取机柜管理列表数据", res);
            resolve(res.rows);
          })
          .catch((err: any) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //遍历数据分出所有字段数组
    getAllFiledsData(data: any[]) {
      let tempXaxis = <any>[];
      let tempEndData = <any>{};
      for (const key in data[0]) {
        tempEndData[key] = [];
      }
      for (let i = 0, len = data.length; i < len; i++) {
        tempXaxis.push(data[i].createTime);
        for (const key in data[i]) {
          tempEndData[key].push(data[i][key]);
        }
      }
      console.log("遍历数据分出所有字段数组", tempEndData, tempXaxis);

      this.endData = tempEndData;
      this.xAxisData = tempXaxis;
      return true;
    },
    //查询机柜历史数据列表
    getElectricData(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          ElectricData +
            "/list" +
            "?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then(async (res: any) => {
            console.log("查询机柜历史数据列表", res);
            await this.getAllFiledsData(res.rows);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //查询机柜历史数据图表
    getElectricDataEcharts(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          ElectricData +
            "/chartList" +
            "?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then(async (res: any) => {
            console.log("查询机柜历史数据图表", res);
            if (JSON.stringify(res.data) !== "{}") {
              this.endData = res.data;
              this.xAxisData = res.data.createTime;
            }
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //普通过滤机柜数据
    getElectricNormalData(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          AssetCabinet +
            "/list" +
            "?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then((res: any) => {
            console.log("普通过滤机柜数据", res);
            resolve(res.rows);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //设备最新实时数据
    getLast(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          ElectricDataRealTimeLast +
            "/list" +
            "?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then(async (res: any) => {
            console.log("设备最新实时数据", res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    getLastFilter(filter: any) {
      return new Promise((resolve, reject) => {
        _get(ElectricDataRealTimeLast + "/list", filter, "json", "")
          .then(async (res: any) => {
            console.log("设备最新实时数据", res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
